//
//  JumpToMapNavHelper.swift
//  EVOP
//
//  Created by LC_WJH on 2017/8/29.
//  Copyright © 2017年 gmc. All rights reserved.
//

import Foundation
import MapKit

/*
 IOS9后需要在info.plist中添加配置
 
 <key>LSApplicationQueriesSchemes</key>
 <array>
 <string>baidumap</string>
 <string>comgooglemaps</string>
 <string>iosamap</string>
 <string>mqqopensdkapiV2</string>
 <string>mqq</string>
 <string>mqqapi</string>
 </array>
 
 
 
 */


///跳转到地图导航辅助类
public class JumpToMapNavHelper:NSObject {
    
    static var setLocate:CLLocationCoordinate2D?;
    static var placeName:String?;
    
    public static func JumpToMapNav(place:String,location:CLLocationCoordinate2D?,atVC:UIViewController?){
        
        if location == nil || atVC == nil {
            return
        }
        
        placeName = place;
        
        setLocate = location;
         
        let al:UIAlertController = UIAlertController.init(title: "导航到位置", message: "导航到" + place, preferredStyle: UIAlertController.Style.actionSheet);
        
 
        let commands:[(title:String,Scheme:String?,ACB:(UIAlertAction)->Void)] =
        [
            ("苹果地图",nil,{
                (action:UIAlertAction) in
                 JumpToMapNavHelper.goAppleMap();
            }),
            ("百度地图","baidumap://",{
                (action:UIAlertAction) in
                JumpToMapNavHelper.goBaiduMap();
            }),
            ("高德地图","iosamap://",{
                (action:UIAlertAction) in
                JumpToMapNavHelper.goGaoDeMap();
            }),
            ("谷歌地图","comgooglemaps://",{
                (action:UIAlertAction) in
                JumpToMapNavHelper.goGoogleMap();
            })
        ]
        
        for cmd in commands {
            if checkIfSchemeResponseable(schem: cmd.Scheme) {
                al.addAction(UIAlertAction.init(title: cmd.title, style: UIAlertAction.Style.default, handler:cmd.ACB));
            }
        }
        
        al.addAction(UIAlertAction.init(title:"取消", style: UIAlertAction.Style.destructive, handler:nil));
        
        atVC?.present(al, animated: true, completion: nil);
    }
    

    
    
    //跳到苹果地图
    static func goAppleMap(){
        
        let currentLoc:MKMapItem =  MKMapItem.forCurrentLocation();
        let toloc:MKMapItem = MKMapItem.init(placemark: MKPlacemark.init(coordinate: setLocate!, addressDictionary: nil))
        MKMapItem.openMaps(with: [currentLoc,toloc], launchOptions: [
                MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving])
        
        setLocate = nil;
        placeName = nil;
    }
    
    //跳到Baidu地图
    static func goBaiduMap(){
        

        let urlString:String = NSString.init(
            format: "baidumap://map/direction?origin={{我的位置}}&destination=latlng:%f,%f|name=%@&mode=walking&coord_type=gcj02",
            setLocate!.latitude,
            setLocate!.longitude,
            placeName!)
            .addingPercentEscapes(using: String.Encoding.utf8.rawValue)!;
        
        UIApplication.shared.openURL(URL.init(string: urlString)!);
        
        setLocate = nil;
        placeName = nil;
    }
    
    //跳到高德地图
    static func goGaoDeMap(){
        
        let appName =  "小猪出行";
        let urlScheme = "";
        
        let urlString:String = NSString.init(
            format: "iosamap://path?sourceApplication=%@&backScheme=%@&dlat=%f&dlon=%f&dname=%@&dev=0&style=2&t=2",
            appName,
            urlScheme,
            setLocate!.latitude,
            setLocate!.longitude,
            placeName!)
            .addingPercentEscapes(using: String.Encoding.utf8.rawValue)!;
        
        UIApplication.shared.openURL(URL.init(string: urlString)!);
        
        setLocate = nil;
        placeName = nil;
        
    }
    
    //跳到谷歌地图
    static func goGoogleMap(){
        
        let appName =  "小猪出行";
        let urlScheme = "";
        
        let urlString:String = NSString.init(
            format: "comgooglemaps://?x-source=%@&x-success=%@&saddr=&daddr=%f,%f&directionsmode=driving",
            appName,
            urlScheme,
            setLocate!.latitude,
            setLocate!.longitude)
            .addingPercentEscapes(using: String.Encoding.utf8.rawValue)!;
        
        UIApplication.shared.openURL(URL.init(string: urlString)!);
        
        setLocate = nil;
        placeName = nil;
        
    }
    
    
    static func checkIfSchemeResponseable(schem:String?)->Bool{
        
        if schem == nil {
            return true
        }
        
        return UIApplication.shared.canOpenURL(URL.init(string:schem!)!);
       // [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]];
    }
    
}
